package sandbox.patterns.stream;

import java.util.Iterator;
import java.util.NoSuchElementException;

class StreamIterator<E> implements Iterator<E> {

    private Stream<E> current;

    public StreamIterator(Stream<E> stream) {
        super();
        current = stream;
    }

    @Override
    public boolean hasNext() {
        return current != Stream.empty();
    }

    @Override
    public E next() {
        if (current == Stream.empty()) {
            throw new NoSuchElementException("End of stream reached.");
        }
        E headValue = current.head();
        current = current.tail();
        return headValue;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
